﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Text;
using System.Net;
using Dinamico.Models;


namespace Dinamico.Service
{
    public class SmtpMailService : IMailService
    {

        #region Private Members

        private const string MailSubjectFormat = "{0}Subject";
        private const string MailBodyFormat = "{0}Body";
        private const string MailParameterFormat = "{{{0}}}";

        private ILogger log;

        private SmtpClient GetSmtpClient()
        {
            return new SmtpClient();
        }



        private string ReplaceParameters(string content, IDictionary<string, string> toReplaceParams)
        {
            if( string.IsNullOrEmpty(content) )
                throw new ArgumentException("Cannot be null or empty", "content");
            if( toReplaceParams == null )
                throw new ArgumentNullException("toReplaceValues");

            foreach( KeyValuePair<string, string> toReplaceParam in toReplaceParams )
            {
                content = content.Replace(string.Format(MailParameterFormat, toReplaceParam.Key), toReplaceParam.Value);
            }

            return content;
        }

        #endregion

        public SmtpMailService(ILogger logger)
        {
            this.log = logger;
        }

        #region IMailService Members

        private void SendMail(string subiect, string body, string to, string toDisplayName)
        {
            using( MailMessage message = new MailMessage() )
            {
                try
                {
                    var senderMailAddress = "robot@X.com";
                    var senderDisplayName = "X.com";

                    var bcc1 = "X@o2.pl";
                    var bcc2 = "X@gmail.com";
                    var bcc3 = "X@gmail.pl";

                    message.From = new MailAddress(senderMailAddress, senderDisplayName);
                    
                    message.To.Add(new MailAddress(to, toDisplayName));
                    
                    message.Bcc.Add(new MailAddress(bcc1, toDisplayName));
                    message.Bcc.Add(new MailAddress(bcc2, toDisplayName));
                    message.Bcc.Add(new MailAddress(bcc3, toDisplayName));

                    message.Priority = MailPriority.Normal;

                    message.Subject = subiect;
                    message.SubjectEncoding = Encoding.UTF8;

                    message.BodyEncoding = Encoding.UTF8;
                    message.IsBodyHtml = false;//TODO: add possibility to send html messages
                    message.Body = body;
                    SmtpClient smtpClient = GetSmtpClient();
                    smtpClient.Send(message);
                }
                catch( SmtpException ex )
                {
                    log.Error(ex);//TODO: more details to log?
                }
            }
        }

        public void SendOrderMail(JsonAddressModel model, List<BasketItemModel> basketItems)
        {
            SendMail(OrderSubiect(), OrderBody(basketItems), model.Email, model.Name); 
        }

        #endregion

        private string OrderBody( List<BasketItemModel> basketItems)
        {
            StringBuilder body = new StringBuilder();
            double sum = 0;
            foreach (var item in basketItems) {
                var itemValue  = item.Quantity * item.Price;
                sum+=itemValue;
                body.AppendLine(string.Format(" {0} |{1} |{2} |{3}", item.Name, item.Quantity, item.Price, itemValue));
            }

            body.AppendLine(string.Format(" suma: {0} ", sum));
            body.Append(OrderFooter());
            
            return body.ToString();
        }

        private string OrderSubiect()
        {
            return "Your header";
        }

        private string OrderFooter()
        {
            return "Your footer"; 
        }

        private string OrderHeader(JsonAddressModel model)
        {
            return String.Format("Witaj {0} {1}", model.Name, model.Surname );
        }
    
    }
}